package com.example.hucanhui.keyboard;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.RelativeLayout;


public class MainActivity extends Activity{

    private EditText et_input;
    private RelativeLayout parent;
    private KeyBoardPopup keyBoardPopup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        setContentView(R.layout.activity_main);
        et_input = (EditText) findViewById(R.id.et_input);

        parent = (RelativeLayout) findViewById(R.id.parent);
        keyBoardPopup = new KeyBoardPopup(this, parent, et_input);

        int[] ids = new int[]{
                R.id.tv_dounble_0, R.id.tv_0, R.id.tv_1, R.id.tv_2, R.id.tv_3, R.id.tv_4,
                R.id.tv_5, R.id.tv_6, R.id.tv_7, R.id.tv_8, R.id.tv_9, R.id.tv_sifenzhiyi,
                R.id.tv_sanfenzhi, R.id.tv_half, R.id.tv_all, R.id.tv_delete, R.id.tv_hide,
                R.id.tv_empty, R.id.tv_finish
        };

        keyBoardPopup.setKeyBoardView(this, 250,R.layout.layout_keyboard, ids);

        keyBoardPopup.setOnKeyDownListener(new KeyBoardPopup.OnKeyDownListener() {
            @Override
            public void onKeyDown(View v) {

                switch (v.getId()) {

                    case R.id.tv_dounble_0:
                        keyBoardPopup.append2EditText("00");
                        break;
                    case 1000:
                        keyBoardPopup.append2EditText("0");
                    case R.id.tv_0:
                        keyBoardPopup.append2EditText("0");
                        break;
                    case R.id.tv_1:
                        keyBoardPopup.append2EditText("1");
                        break;
                    case R.id.tv_2:
                        keyBoardPopup.append2EditText("2");
                        break;
                    case R.id.tv_3:
                        keyBoardPopup.append2EditText("3");
                        break;
                    case R.id.tv_4:
                        keyBoardPopup.append2EditText("4");
                        break;
                    case R.id.tv_5:
                        keyBoardPopup.append2EditText("5");
                        break;
                    case R.id.tv_6:
                        keyBoardPopup.append2EditText("6");
                        break;
                    case R.id.tv_7:
                        keyBoardPopup.append2EditText("7");
                        break;
                    case R.id.tv_8:
                        keyBoardPopup.append2EditText("8");
                        break;
                    case R.id.tv_9:
                        keyBoardPopup.append2EditText("9");
                        break;
                    case R.id.tv_sifenzhiyi:
                        keyBoardPopup.setTextForEditText("550");
                        break;
                    case R.id.tv_sanfenzhi:
                        keyBoardPopup.setTextForEditText("773");
                        break;
                    case R.id.tv_half:
                        keyBoardPopup.setTextForEditText("1100");
                        break;
                    case R.id.tv_all:
                        keyBoardPopup.setTextForEditText("2200");
                        break;
                    case R.id.tv_delete:
                        keyBoardPopup.delete();
                        break;
                    case R.id.tv_hide:
                        keyBoardPopup.dismiss();
                        break;
                    case R.id.tv_empty:
                        et_input.setText("");
                        break;
                    case R.id.tv_finish:
                        keyBoardPopup.dismiss();
                        break;
                }
            }
        });

        et_input.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!keyBoardPopup.isShowing()) {
                    keyBoardPopup.showKeyBoard();
                }
            }
        });
    }


    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        keyBoardPopup.setYouNeed2InterceptView(ev, et_input);
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public void onBackPressed() {
        if(keyBoardPopup.isShowing()){
            keyBoardPopup.dismiss();
        }else {
            super.onBackPressed();
        }
    }
}
